import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '../components/login.vue'
import home from '../components/home.vue'
import welcome from '../components/welcome.vue'
// 用户
import user from '../components/user/users.vue'
// 权限
import right from '../components/Authority/rights.vue'
import roles from '../components/Authority/roles.vue'
// 商品
import categories from '../components/commodity/categories.vue'
import goods from '../components/commodity/goods.vue'
import params from '../components/commodity/params.vue'
// 订单
import orders from '../components/Order/orders.vue'
// 数据
import report from '../components/Data/reports.vue'
Vue.use(VueRouter)

const routes = [
  { path: '/', redirect: '/login' },
  { path: '/login', name: 'Login', component: Login },
  {
    path: '/home',
    name: 'home',
    component: home,
    redirect: '/welcome',
    children: [
      // 欢迎
      { path: '/welcome', component: welcome },
      // 用户
      { path: '/users', component: user },
      // 权限
      { path: '/roles', component: roles },
      { path: '/rights', component: right },
      // 商品
      { path: '/categories', component: categories },
      { path: '/goods', component: goods },
      { path: '/params', component: params },
      // 订单
      { path: '/orders', component: orders },
      // 数据
      { path: '/reports', component: report }
    ]
  }

  // {
  //   path: '/about',
  //   name: 'About',
  //   // route level code-splitting
  //   // this generates a separate chunk (about.[hash].js) for this route
  //   // which is lazy-loaded when the route is visited.
  //   component: () =>
  //     import(/* webpackChunkName: "about" */ '../views/About.vue')
  // }
]

const router = new VueRouter({
  routes
})
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to将要访问的路径
  // from从那个路径来的
  // next是一个函数 next() 或者 next('/home)
  if (to.path === '/login') return next()
  const tokenstr = window.sessionStorage.getItem('token')
  if (!tokenstr) {
    return next('/login')
  } else {
    next()
  }
})

export default router
